package chen.web.user;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.annotate.JsonIgnore;

import chen.web.IdEntity;
import chen.web.cache.DirtyDataCheckable;

public class Account extends IdEntity implements DirtyDataCheckable, Serializable{

	private static final long serialVersionUID = 5806747788980276934L;
	private static final int Account_Flag_Forbid_Login = 0x1; //禁止登录
	public static final int ACCOUNT_SEX_BOY = 1;
	public static final int ACCOUNT_SEX_GIRL = 2;
	
	public static enum Medal{Good_Member, Authentication};
	private static Log log = LogFactory.getLog(Account.class);

    private String nickname;
    
	private Password password;

    private String email;

    private Date lastOnline;
    
    private Date lastOffline;			//最后一次离线的时间

    private Date created;		//创建的时间

    private int flag = 0;		//扩展标记
    
    private Role role;			//角色
    
    private int rank = 0;
    
    private int onlineSeconds;
    
    private ExtInfo extInfo ;
	
	public Account(int id){
		super(id);
	}
	
	protected Account(){
		super(0);
	}
   
    public String getNickname() {
        return nickname;
    }

    void setNickname(String nickname) {
        this.nickname = nickname;
        setDirty("nickname");
    }

	@JsonIgnore 
    public Password getPassword() {
        return password;
    }

    void setPassword(Password password) {
        this.password = password;
        setDirty("password");
    }

    public String getEmail() {
        return email;
    }


    void setEmail(String email) {
        this.email = email;
        setDirty("email");
    }

    public Date getCreated() {
        return created;
    }

 
    void setCreated(Date created) {
        this.created = created;
        setDirty("Created");
    }
    
    public Role getRole() {
        return role;
    }

    void setRole(Role role) {
        this.role = role;
    	setDirty("role");
    }

	void setFlag(int flag) {
		this.flag = flag;
		setDirty("Flag");
	}


	int getFlag() {
		return flag;
	}
	
	public boolean isForbidLogin(){
		return (flag & Account_Flag_Forbid_Login) == 1;
	}
	
	public void setForbidLogin(boolean forbidLogin){
		if(forbidLogin)
			flag = flag | Account_Flag_Forbid_Login;
		else 
			flag = flag & (~Account_Flag_Forbid_Login);
		setDirty("forbidLogin");
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (id != other.id)
			return false;
		if(id == 0 || other.id == 0){
			return false;
		}
		return true;
	}

	void setRank(int rank){
		this.rank = rank;
		setDirty("rank");
	}

	public int getRank() {
		return rank;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String result = new StringBuffer().append("Account:id=")
										.append(id)
										.append(",nickname=")
										.append(nickname)
										.toString();
		return result;
	}

	
	private boolean ditry = true;
	@Override
	@JsonIgnore 
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return ditry;
	}

	@Override
	public void clean() {
		// TODO Auto-generated method stub
		ditry = false;
	}
	
	private void setDirty(String property){
		if(!ditry){
			ditry = true;
			if(log.isDebugEnabled()){
				log.debug("account is dirty for setting property: " + property);
			}	
		}
	}

	void setLastOffline(Date lastOffline) {
		this.lastOffline = lastOffline;
	}

	public Date getLastOffline() {
		return lastOffline;
	}

	void setOnlineSeconds(int onlineSeconds) {
		this.onlineSeconds = onlineSeconds;
	}
	
	synchronized void increaseOnlineSeconds(int onlineSeconds){
		this.onlineSeconds = this.onlineSeconds + onlineSeconds;
	}

	public int getOnlineSeconds() {
		return onlineSeconds;
	}

	void setExtInfo(ExtInfo extInfo){
		extInfo.setAccount(this);
		this.extInfo = extInfo;
	}
	
	public ExtInfo getExtInfo() {
		if(extInfo == null){
			extInfo = new ExtInfo(this);
		}
		return extInfo;
	}

	public Date getLastOnline() {
		return lastOnline;
	}

	void setLastOnline(Date lastOnline) {
		this.lastOnline = lastOnline;
	}

}